package com.leetcode.p200;

import com.bean.ListNode;
import com.utils.DataUtils;

public class Test234 {

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode l1 = head;
        ListNode l2 = slow.next;
        slow.next = null;

        l2 = reserve(l2);

        while (l1 != null && l2 != null) {
            if (l1.val != l2.val) {
                return false;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        return true;
    }

    private ListNode reserve(ListNode listNode) {
        ListNode pre = null;
        ListNode cur = listNode;

        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;

            pre = cur;

            cur = next;
        }

        return pre;
    }

    public static void main(String[] args) {
//        ListNode listNode = DataUtils.getListNode(new int[]{0, 0});
        ListNode listNode = DataUtils.getListNode(new int[]{0, 1, 1, 0});

        Test234 test = new Test234();
        boolean result = test.isPalindrome(listNode);
        System.out.println(result);
    }

}
